package com.cy.mall.service.message.impl;

import com.cy.mall.comm.annotation.Message;
import com.cy.mall.comm.constant.Constants;
import com.cy.mall.dao.mapper.order.OrderMapper;
import com.cy.mall.model.enums.MessageType;
import com.cy.mall.service.BaseService;
import com.cy.mall.service.message.MessageService;
import com.cy.mall.service.wechat.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 消息service唯一实现类
 *
 * @author: tanyilin
 * @create: 2018年12月11日 下午4:03:12
 */
@Service("messageService")
@Primary
public class MessageServiceImpl extends BaseService implements MessageService {

    @Value("${app.home.page}")
    protected String appHomePage;
    @Autowired
    protected WechatService wechatService;
    @Autowired
    protected OrderMapper orderMapper;

    private static Map<MessageType, MessageServiceImpl> messageServiceMap = new HashMap<>();

    /**
     * 初始化messageServiceMap
     */
    @PostConstruct
    public void init() {
        Message message = this.getClass().getAnnotation(Message.class);
        if(message == null) {
            return;
        }
        messageServiceMap.put(message.type(), this);
    }

    @Override
    @Async(Constants.ASYNC)
    public void sendMessage(String message, MessageType messageType) {
        //执行发送消息的方法
        if(messageServiceMap.get(messageType) != null) {
            messageServiceMap.get(messageType).execute(message);
        }
    }

    /**
     * 真正执行发送消息的方法
     *
     * @param message
     */
    public void execute(String message) {

    }

}
